<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Homework;
use think\Db;

/**
 * 新概念接口
 */
class Newconcept extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $sp = 'new_concept_products';//新概念 商品教材表
    protected $sc = 'new_concept_catalogues';//新概念 章节表
    protected $sr = 'new_concept_resources';//新概念 资源表
    protected $asd = 'association_data';//关联数据设置表
    /**
     * 新概念列表
     *
     */
    public function index()
    {
        $grade = input('grade/d'); //年级
        $key = input('key/s');
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        $assarr = [];
        $assarr = Db::name($this->asd)->where(['type'=>'newconcept','status'=>2])->column('rid');
        $where = [];
        if ($grade) {
          $where['grade'] = $grade;  
        }
        if ($key) {
            $where['productName|bookIntro'] = ['like','%'.$key.'%'];
        }
        $spdata = Db::name($this->sp)->where($where)->whereNotIn('productId',$assarr)->order('id asc')->limit(($page - 1)*$pageSize,$pageSize)->select();
        $parr = Db::name('teacher_collect')->where(['uid'=>$this->auth->id,'status'=>1])->column('pid');
        foreach ($spdata as &$vs) {
            $vs['collect'] = 0;
            if (in_array($vs['productId'],$parr)) {
                $vs['collect'] = 1;
            }
            $vs['cover_full'] = cdnurl($vs['cover'],true);
        }
        unset($vs);
        $this->success('请求成功',$spdata);
    }
    /**
     * 新概念详情
     *
     */
    public function detail()
    {
        $id = input('id/d'); //新概念商品id
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['productId'] = $id;  
        }
        $parr = Db::name('teacher_collect')->where(['uid'=>$this->auth->id,'status'=>1])->column('pid');
        $spdata = Db::name($this->sp)->where($where)->find();
        $spdata['collect'] = 0;
        if (in_array($spdata['productId'],$parr)) {
            $spdata['collect'] = 1;
        }
        $spdata['cover_full'] = cdnurl($spdata['cover'],true);
        //$spdata['sc'] = Db::name($this->sc)->where($where)->order('displayOrder desc')->select();
        //$spdata['sc_num'] = count($spdata['sc']);
        $this->success('请求成功',$spdata);
    }

    /**
     * 新概念章节列表
     *
     */
    public function chapterlist()
    {
        $id = input('id/d'); //新概念商品id
        $order = input('order/s')?input('order/s'):'asc'; //新概念商品章节排序 升序：asc 降序：desc
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['productId'] = $id;  
        }
        if ($order) {
            $order = 'displayOrder '.$order;
        }
        $scdata = Db::name($this->sc)->where($where)->order($order)
            /*->limit(($page - 1)*$pageSize,$pageSize)*/
            ->select();
        
        $this->success('请求成功',$scdata);
    }

    /**
     * 新概念章节详情
     *
     */
    public function chapterdetail()
    {
        $id = input('id/d'); //新概念商品章节id
        $pid = input('pid/d');//新概念商品教材id
        if (!$id&&!$pid) {
            $this->error(__('Invalid parameters'));
        }
        $scdata = Db::name($this->sc)->where(['id'=>$id,'productId'=>$pid])->find();
        if($scdata['gunzipResPath'] =='#'){
            $sr = Db::name($this->sr)->where(['cId'=>$id,'productId'=>$pid,'state'=>1])->order('id asc')->select();
            foreach ($sr as &$vs) {
                $vs['gunzipResPath_full'] = fileurl($vs['gunzipResPath'].'/resource/resource.json',true);
                $resource = json_decode(file_get_contents($vs['gunzipResPath_full']),true);
                $vs['tnum'] = isset($resource['parts'])?count($resource['parts']):0;;
                $vs['totalScore'] = isset($resource['ref_score'])?$resource['ref_score']:0;
                $vs['minute'] = $vs['tnum']*3;
                $vs['scoringrate'] = '0%';
                $vs['usenum'] = 1;
            }
            unset($vs);
        }else{
            $sr = sr($scdata['gunzipResPath'],'new_concept');
        }
        $rcdata['sr'] = $sr;
        $this->success('请求成功',$rcdata);
    }

    /**
     * 新概念章节详情 教师端使用 戎工
     *
     */
    public function chapterdetail_teacher()
    {
        $id = input('id/d'); //新概念商品章节id
        $pid = input('pid/d');//新概念商品教材id
        if (!$id&&!$pid) {
            $this->error(__('Invalid parameters'));
        }
        $homework = new Homework();
        $scdata = Db::name($this->sc)->where(['id'=>$id,'productId'=>$pid])->find();
        $sr = Db::name($this->sr)->where(['cId'=>$id,'productId'=>$pid])->order('id asc')->select();
        foreach ($sr as &$vs) {
            $vs['gunzipResPath_full'] = fileurl('/file/new_concept/res/'.$vs['gunzipResPath'],true);
            $resource = json_decode(file_get_contents($vs['gunzipResPath_full']),true);
            $vs['tnum'] = isset($resource['parts'])?count($resource['parts']):count($resource);
            $vs['totalScore'] = isset($resource['ref_score'])?$resource['ref_score']:0;
            $vs['minute'] = $vs['tnum']*3;
            $vs['scoringrate'] = '0%';
            $vs['usenum'] = $homework->where(['uid'=>$this->auth->id,'ridjson'=>['like','%'.$id.'%'],'status'=>1])->count();
        }
        unset($vs);
        $rcdata['sr'] = $sr;
        $this->success('请求成功',$rcdata);
    }

    /**
     * 新概念模拟测试题
     *
     */
    public function questions()
    {
        $id = input('id/s'); //新概念商品测试题id
        $rest = input('rest'); //新概念商品测试题id
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $hsr = Db::name($this->sr)->where('versionId',$id)->find();
        $hsr['gunzipResPath_full'] = cdnurl('/file/new_concept/res/',true).$hsr['gunzipResPath'];
        $resource = json_decode(file_get_contents($hsr['gunzipResPath_full']),true);
        $data = [
            'resmain' => fileurl('/file/new_concept/res/',true),
            'resource'=>$resource,
            // 'resource_h5'=>$resource_h5,
        ];
        $this->success('请求成功',$data);
    }
}
